package cn.tedu.csmall.all.adapter.service.impl;

import cn.tedu.csmall.all.service.ICartService;
import cn.tedu.csmall.commons.pojo.order.dto.OrderAddDTO;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
远程调用时,和cart购物车有关的方法
 单独封装一个bean对象
 */
@Component
@Slf4j
public class DubboCartService {
    @DubboReference
    private ICartService cartService;

    /**
     * 熔断降级的资源方法
     * @param orderAddDTO
     */
    @SentinelResource(
            value="cartDelete",
            blockHandler ="cartDeleteBlock" ,
            fallback = "cartDeleteFallback")
    public void cartDelete(OrderAddDTO orderAddDTO){
        cartService.deleteUserCart(orderAddDTO.getUserId(), orderAddDTO.getCommodityCode());
    }
    public void cartDeleteBlock(OrderAddDTO orderAddDTO, BlockException e){
        log.error("熔断规则生效,断路器打开状态,orderAddDTO:{},异常信息e:{}",orderAddDTO,e.getMessage());
    }
    public void cartDeleteFallback(OrderAddDTO orderAddDTO,Throwable e){
        log.error("业务调用异常,orderAddDTO:{},异常信息e:{}",orderAddDTO,e.getMessage());
    }
}
